# Contributor: Ramanathan Sivagurunathan <ramzthecoder@gmail.com>
# Maintainer:
pkgname=elastic-beats
pkgver=8.14.2
pkgrel=0
pkgdesc="Lightweight shippers for Elasticsearch & Logstash"
url="https://www.elastic.co/products/beats"
# riscv64: https://github.com/elastic/go-concert/issues/51
# s390x: doesn't build
# 32-bit: not useful
# loongarch64: no loongarch64 support
# x86_64: ftbfs on builder
arch="all !s390x !armhf !armv7 !x86 !riscv64 !loongarch64 !x86_64"
license="Apache-2.0"
makedepends="
	go
	mage
	python3
	"
subpackages="
	filebeat
	filebeat-openrc:filebeat_openrc:noarch
	metricbeat
	metricbeat-openrc:metricbeat_openrc:noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/elastic/beats/archive/refs/tags/v$pkgver.tar.gz
	filebeat.initd
	filebeat.confd
	metricbeat.initd
	metricbeat.confd
	"
builddir="$srcdir/beats-$pkgver"
options="net" # go

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

case "$CARCH" in
x86_64) ;;
# flaky elsewhere
*) options="$options !check" ;;
esac

prepare() {
	default_prepare

	git init -q
	git config user.email "example@example.com"
	git config user.name "example"
	git add .
	git commit -q -m "init"
	git tag "v$pkgver"
}

build() {
	mkdir -p bin

	# just these two for now
	(
		msg "building filebeat"
		cd filebeat
		# mage build
		go build -o filebeat main.go
		# mage update
	)
	(
		msg "building metricbeat"
		cd metricbeat
		# mage build
		go build -o metricbeat main.go
		# fails in some random step presently
		# mage update || :
	)
}

check() {
	msg "testing filebeat"
	# shellcheck disable=2046
	go test $(go list ./filebeat/... | grep -Ev '(journald|inputsource/unix)')
	msg "testing metricbeat"
	# shellcheck disable=2046
	go test $(go list ./metricbeat/... | grep -Ev '(module/system/service)')
}

package() {
	mkdir -p "$pkgdir"/usr/share/metricbeat "$pkgdir"/usr/share/filebeat

	install -Dm644 \
		filebeat/filebeat.yml \
		filebeat/filebeat.reference.yml \
		-t "$pkgdir"/etc/filebeat
	cp -r filebeat/modules.d "$pkgdir"/etc/filebeat/
	cp -r filebeat/module "$pkgdir"/usr/share/filebeat/

	install -Dm644 \
		metricbeat/metricbeat.yml \
		metricbeat/metricbeat.reference.yml \
		-t "$pkgdir"/etc/metricbeat
	cp -r metricbeat/modules.d "$pkgdir"/etc/metricbeat/
	cp -r metricbeat/module "$pkgdir"/usr/share/metricbeat/

	# remove random stuff
	local elf; scanelf -BRA -F "%F" "$pkgdir"/usr/share | while read -r elf; do
		rm -v "$elf"
	done

	install -Dm755 \
		filebeat/filebeat \
		metricbeat/metricbeat \
		 -t "$pkgdir"/usr/bin/
}

filebeat() {
	pkgdesc="FileBeat - Lightweight shipper for logs"

	amove \
		usr/bin/filebeat \
		etc/filebeat \
		usr/share/filebeat
}

filebeat_openrc() {
	pkgdesc="FileBeat - Lightweight shipper for logs (OpenRC service files)"
	install_if="filebeat=$pkgver-r$pkgrel openrc"

	install -Dm755 "$srcdir"/filebeat.initd "$subpkgdir"/etc/init.d/filebeat
	install -Dm644 "$srcdir"/filebeat.confd "$subpkgdir"/etc/conf.d/filebeat
}

metricbeat() {
	pkgdesc="MetricBeat - Lightweight shipper for system and service statistics"

	amove \
		usr/bin/metricbeat \
		etc/metricbeat \
		usr/share/metricbeat
}

metricbeat_openrc() {
	pkgdesc="MetricBeat - Lightweight shipper for system and service statistics (OpenRC service files)"
	install_if="metricbeat=$pkgver-r$pkgrel openrc"

	install -Dm755 "$srcdir"/metricbeat.initd "$subpkgdir"/etc/init.d/metricbeat
	install -Dm644 "$srcdir"/metricbeat.confd "$subpkgdir"/etc/conf.d/metricbeat
}

sha512sums="
d882e0b0bd8b4bfbc8fb08ff273f02640c065b6526a15f29c662f767ba6b113d8a8a4e5af7d0fbcd13113fb85536baeb3f74341fcfac6c4a11efcc05fb95abf5  elastic-beats-8.14.2.tar.gz
526b149419cf51e21ab4b7b49b208dbef81571afd04f2c40ab05297d5eb4b03e64e6c7a06d1acf8771b7b0664a552082ce6f50d5cbc4218aca810440d6185990  filebeat.initd
f6168b10da75919cf16b2a32a5b25847e9e49aaea7415e49d2644960fc586c60714754cbe60f5a6d290cd7b9f8d3fd86287e46e63891825feb82f747e235d58d  filebeat.confd
4736fab0119bea080cb5a938d0b40e803967cbfac373d1b714a43176032ea36b9865f3c6658e33ff6befa150ccdf6ddcbfcf59bd31e4ae961d075623eac80ea6  metricbeat.initd
a81cd8a99acd9086b78f36113f08f8fb91373144dc51aa6b4235362e25bc4ff3b1f5ebfe6efe272a106a73d16c1c9d23dd1da6645074d4fd2d3b8713b6071f68  metricbeat.confd
"
